package com.mew.game.chess;

import com.mew.common.dc.db.LoginoutService;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.dc.db.WinFreeService;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.enm.ExitType;
import com.mew.common.entity.game.WinFree;
import com.mew.common.entity.user.Profile;
import com.mew.common.rest.bo.MewSession;
import com.mew.common.rest.game.ChessEnterRoomRequest;
import com.mew.common.rest.game.ChessEnterRoomResponse;
import com.mew.common.rest.sas.EnterGameSasRequest;
import com.mew.common.rest.sas.ExitGameSasRequest;
import com.zebra.common.OnlineType;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ChessEnterRoomService {

    @Autowired
    private WinFreeService winFreeService;
    @Autowired
    private LoginoutService loginoutService;
    @Autowired
    private ProfileService profileService;
    @Autowired
    private AucRedisService aucRedisService;

    public ChessEnterRoomResponse enterRoom(ChessEnterRoomRequest request){
        MewSession session = aucRedisService.getSession(request.getUid());
        Profile profile = profileService.getProfileById(request.getUid());
        if (-9 == session.rioid){
            return doEnterRoom(request, session, profile);
        }
        if (request.getType() == 3){
            return doReconnectRoom(request, session, profile);
        }
        loginoutService.doExitRoom(session, profile, ExitType.Repeat);
        return doEnterRoom(request, session, profile);
    }

    private ChessEnterRoomResponse doEnterRoom(ChessEnterRoomRequest request, MewSession session, Profile profile){
        long inoutRoomId = loginoutService.createChessRoomInOut(request.getGameId(), request.getRoomId(), request.getTableId(), request.getSeatNo(), request.getServerId(), session.dtp, profile);
        doOnlineGame(request, inoutRoomId, request.getType());
        return loadInfo(request, profile.getGoldNumber());
    }

    private ChessEnterRoomResponse doReconnectRoom(ChessEnterRoomRequest request, MewSession session, Profile profile){
        doOnlineGame(request, session.rioid, 3);
        return loadInfo(request, profile.getGoldNumber());
    }

    private void doOnlineGame(ChessEnterRoomRequest request, long roomInoutId, int enterType){
        MewSession other = new MewSession();
        other.gwsid = request.getGatewaySid();
        other.tk = aucRedisService.getToken();
        other.loc = request.getGameId();
        other.gsid = request.getServerId();
        other.onl =  OnlineType.Online.value();

        other.gid =  request.getGameId();
        other.rid = request.getRoomId();
        other.tid = request.getTableId();
        other.sno = request.getSeatNo();

        if (enterType != 3){
            other.initPlayInfo();
            other.rioid = roomInoutId;
        }
        aucRedisService.onlineSession(request.getUid(), other);
    }

    private ChessEnterRoomResponse loadInfo(ChessEnterRoomRequest request, long goldNumber){
       WinFree winFree = winFreeService.getWinFree(request.getUid(), request.getGameId(), request.getRoomId());

        EventBusAsyncCenter.post(EnterGameSasRequest.builder()
                .userId(request.getUid())
                .gameId(request.getGameId())
                .roomId(request.getRoomId())
                .build()); //TODO SAS

        return ChessEnterRoomResponse.builder()
                .goldNumber(goldNumber)
                .errorState(200)
                .lastBetNumber(winFree.getLastBetNumber())
                .build();
    }


    public void exitGame(long userId, int exitType){
        MewSession session = aucRedisService.getSession(userId);
        loginoutService.exitRoom(userId, exitType);

        EventBusAsyncCenter.post(ExitGameSasRequest.builder()
                .userId(userId)
                .gameId(session.gid)
                .roomId(session.rid)
                .bet(session.bt)
                .payment(session.pmt)
                .build());  //TODO  推出游戏
    }

}
